#include "cinder/app/AppBasic.h"
#include "cinder/Surface.h"
#include "cinder/gl/Texture.h"
#include "cinder/qtime/QuickTime.h"
#include "cinder/Text.h"
#include "cinder/Utilities.h"
#include "cinder/ImageIo.h"
#include "cinder/Camera.h"
#include "cinder/params/Params.h"
#include "cinder/gl/Vbo.h"
#include "cinder/gl/Light.h"
#include "cinder/gl/Material.h"
#include "cinder/gl/Fbo.h"
#include <ExtOSCThread.h>
#include "cinder/gl/GlslProg.h"
#include "Resource.h"
#include "Global.h"
#include "Chat.h"

//#include "cinder/"
//Area between two intersecting lines becomces "accrued" and starts changing in some fashion

using namespace cinder;
using namespace cinder::app;
using namespace std;

class CurseOfYig : public AppBasic {

public:
    void setup();
    void keyDown( KeyEvent event );
    void update();
    void draw();
    void prepareSettings( Settings *settings );
    void feedback();
    void feedback2();
    void drawObjects();
    void blur();

private:
    ExtOSCThread* oscThread;

    // Constants
    /*
    static const int FBO_WIDTH = 1440;
    static const int FBO_HEIGHT = 1050;
    static const int BLUR_WIDTH = 320;
    static const int BLUR_HEIGHT = 240;
    */

    /*
    static const int FBO_WIDTH = 1280;
    static const int FBO_HEIGHT = 1024;
    static const int BLUR_WIDTH = 320;
    static const int BLUR_HEIGHT = 262;
    */

    /*
    static const int FBO_WIDTH = 1024;
    static const int FBO_HEIGHT = 768;
    static const int BLUR_WIDTH = 320;
    static const int BLUR_HEIGHT = 240;
    */

    int FBO_WIDTH;
    int FBO_HEIGHT;
    int BLUR_WIDTH;
    int BLUR_HEIGHT;

    static const int ITERATIONS = 4;   // Should always be even

    gl::Fbo::Format format;
    gl::Fbo feed,feed2,mFboScene,mFboBlur1,mFboBlur2;
    // Shaders
    gl::GlslProg mShaderBlur;
    gl::GlslProg textureShader;
    float rotateAmt;
    float redDelta;
    float greenDelta;
    float blueDelta;
    gl::Texture feedTexture,feedTexture2;
    float prevMouseX,prevMouseY;
    gl::VboMesh blendQuad;
    Settings *settings;
    gl::Texture* glLogo;
    bool drawLogo;
    bool twoPLUS;
};

void CurseOfYig::prepareSettings( Settings *settings ){


    settings->setFrameRate( 60.0f );
//    settings->enableSecondaryDisplayBlanking(false);
//    settings->setDisplay( ci::Display::getDisplays()[1] );
    settings->setFullScreen(false);
//    FBO_WIDTH = settings->getDisplay()[1].getWidth();
//    FBO_HEIGHT = settings->getDisplay()[1].getArea().getHeight();\
    FBO_WIDTH = 1280;
    FBO_HEIGHT = 1024;
    BLUR_WIDTH = FBO_WIDTH*0.25;
    BLUR_HEIGHT = FBO_HEIGHT*0.25;
    FBO_WIDTH = 1280;
    FBO_HEIGHT = 1024;
    settings->setWindowSize( FBO_WIDTH, FBO_HEIGHT );
    FBO_WIDTH = 1280;
    FBO_HEIGHT = 1024;
    BLUR_WIDTH = FBO_WIDTH*0.25;
    BLUR_HEIGHT = FBO_HEIGHT*0.25;
    std::cout << "FBO_WIDTH "  << FBO_WIDTH << std::endl;
    std::cout << "FBO_HEIGHT " << FBO_HEIGHT << std::endl;
    std::cout << "BLUR_WIDTH " << BLUR_WIDTH << std::endl;
    std::cout << "BLUR_HEIGHT " << BLUR_HEIGHT << std::endl;

    //settings->getDisplay()
    //settings->setDisplay();
//    settings->setFullScreen(true);
    Curse::screenWidth = FBO_WIDTH;
    Curse::screenWidth = FBO_HEIGHT;
}

void CurseOfYig::setup()
{
    twoPLUS = false;
    srand(time(NULL));
    Curse::SECTION =1;
    rotateAmt = 0.5;
    redDelta = 0.1f;
    blueDelta = 0.1f;
    greenDelta = 0.1f;

    //setFullScreen( false );
    oscThread = new ExtOSCThread(32254);
    oscThread->login();


    glEnable(GL_LINE_SMOOTH);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
    gl::enableAlphaBlending();

    // Load shaders
    mShaderBlur = gl::GlslProg(loadResource("/resources/blur_vert.glsl"), loadResource("/resources/blur_frag.glsl"));
    textureShader = gl::GlslProg(loadResource("/resources/texture_vert.vert"), loadResource("/resources/texture_frag.glsl"));
    //vBlur = gl::GlslProg(loadResource("/resources/vBlurVert.glsl"), loadResource("/resources/vBlurFrag.glsl"));

    //Feedback FBO Format setup
    gl::Fbo::Format feedFormat;
    feedFormat.setSamples(4);
    feedFormat.setCoverageSamples(32);
    feedFormat.enableMipmapping(false);
    feedFormat.enableColorBuffer(true);
    feedFormat.enableDepthBuffer(true);
    feedFormat.setWrap(GL_REPEAT, GL_REPEAT);

    // Set up frame buffer objects
    feed = gl::Fbo(FBO_WIDTH,FBO_HEIGHT,feedFormat);
    feed2 = gl::Fbo(FBO_WIDTH,FBO_HEIGHT,feedFormat);

    //Since We're doing feedback, need to do an initial clear of the videocard
    feed.bindFramebuffer();
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    feed.unbindFramebuffer();

    feed2.bindFramebuffer();
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    feed2.unbindFramebuffer();

    //Setup Blur buffers
    gl::Fbo::Format blurFormat;
    blurFormat.enableMipmapping(false);
    //blurFormat.setCoverageSamples(16);
    //blurFormat.setSamples(4);
    blurFormat.enableColorBuffer(true);
    blurFormat.enableDepthBuffer(true);
    blurFormat.setWrap(GL_CLAMP_TO_BORDER_ARB, GL_CLAMP_TO_BORDER_ARB);
    mFboScene = gl::Fbo(FBO_WIDTH, FBO_HEIGHT, blurFormat);
    mFboBlur1 = gl::Fbo(BLUR_WIDTH, BLUR_HEIGHT, blurFormat);
    mFboBlur2 = gl::Fbo(BLUR_WIDTH, BLUR_HEIGHT, blurFormat);

    try {
        glLogo = new gl::Texture(loadImage(loadResource("GlitchLichLogo.png")));
    } catch( ... ) {
        std::cout << "Unable to load logo texture" << std::endl;
    }

    drawLogo = true;
//    Curse::SECTION = 2;
//    oscThread->setSection(2);
}

void CurseOfYig::keyDown( KeyEvent event )
{
    if(event.getChar()  == '1') {
        Curse::SECTION = 1;
        oscThread->setSection(1);
    } else if(event.getChar()  == '2') {
        glBlendFunc (GL_ONE_MINUS_SRC_COLOR,GL_DST_COLOR);
        feed.bindFramebuffer();
        gl::clear(ColorA(0,0,0,0.025));
        feed.unbindFramebuffer();
        Curse::SECTION = 2;
        oscThread->setSection(2);
    } else if(event.getChar()  == '3') {
        glBlendFunc (GL_ONE_MINUS_SRC_COLOR,GL_DST_COLOR);
        feed.bindFramebuffer();
        gl::clear(ColorA(0,0,0,0.025));
        feed.unbindFramebuffer();
        Curse::SECTION = 3;
        oscThread->setSection(3);
    }  else if(event.getChar()  == 'f') {
        //settings->setFullScreen(true);
    } else if(event.getChar() == 'l') {
        drawLogo = !drawLogo;
    } else if(event.getChar() == 't') {
        twoPLUS = !twoPLUS;
    }
}

void CurseOfYig::update()
{
    rotateAmt = rotateAmt+0.01f;
    redDelta = redDelta + 0.01f;
    blueDelta = blueDelta + 0.05f;
    greenDelta = greenDelta + 0.025f;

}

void CurseOfYig::draw()
{

    glEnable( GL_TEXTURE_2D );
    glEnable (GL_BLEND);

    switch(Curse::SECTION) {
    case 1:
        //Curse::SECTION 1
        glBlendFunc (GL_SRC_COLOR,GL_ONE_MINUS_DST_COLOR);
        break;
    case 2:
        //Curse::SECTION 2
        glBlendFunc (GL_ONE_MINUS_SRC_COLOR,GL_DST_COLOR);
        break;
    case 3:
        //Curse::SECTION 3
        //glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_DST_ALPHA);
        glBlendFunc (GL_ONE_MINUS_DST_COLOR,GL_SRC_COLOR);
        //glBlendFunc (GL_ONE_MINUS_SRC_COLOR,GL_DST_COLOR);
        break;
    }


    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    feed.bindFramebuffer();
    feedback();
    drawObjects();
    feed.unbindFramebuffer();

    //gl::draw(feed.getTexture());


    blur();
    //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    //gl::disableAlphaBlending();
    //glDisable(GL_BLEND);
    //glBlendFunc (GL_SRC_COLOR,GL_ONE_MINUS_DST_COLOR);
    //glAlphaFunc(GL_GREATER, 0.5);
    //glEnable(GL_ALPHA_TEST);
    oscThread->drawMetaObject();

    glLogo->bind();
    //gl::color(ColorA(0,0,0,0));

    if(drawLogo==true) {
        gl::enableAlphaBlending(true);
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
        gl::drawSolidRect(Rectf(80,160,80+glLogo->getWidth()*0.5,160+glLogo->getHeight()*0.5));
    }
}


//===============================
//Non-standard methods
//===============================

void CurseOfYig::feedback()
{
    //Create Texture that's a copy of the window, then bind
    feedTexture = feed.getTexture();
    feedTexture.setFlipped(true);

    feedTexture.bind(0);
    feedTexture.enableAndBind();

    //Clear Background, with transparency
    //original
    switch(Curse::SECTION) {
    case 1:
        //Curse::SECTION1
        gl::color(ColorA(1,1,1,0.1));
        break;
    case 2:
        //Curse::SECTION2
        gl::color(ColorA(1,1,1,0.1));
        break;
    case 3:
        //Curse::SECTION3
        gl::color(ColorA(1,1,1,0.9));
        break;
    }

    //rotate feedback
    gl::translate(FBO_WIDTH* 0.5, FBO_HEIGHT* 0.5,0);
    switch(Curse::SECTION) {
    case 1:
        //Curse::SECTION1
        gl::rotate(rotateAmt*0.01f);
        break;
    case 2:
        //Curse::SECTION2
        if(twoPLUS) {
            gl::rotate(rotateAmt*3.0f);
        } else {
            gl::rotate(rotateAmt*((float)oscThread->getObjectCount() + 0.01f));
        }
        break;
    case 3:
        //Curse::SECTION3
        gl::rotate(rotateAmt*1.0f);
        break;
    }
    gl::translate(FBO_WIDTH* -0.5, FBO_HEIGHT* -0.5,0);


    //Map the texture, the unbind

    switch(Curse::SECTION) {
    case 1:
        //Curse::SECTION1
        gl::drawSolidRect( Rectf( FBO_WIDTH* -0.01f, FBO_HEIGHT*(1.01f+ (sin(rotateAmt)*0.00025)), FBO_WIDTH*(1.01f- (sin(rotateAmt)*0.00025)), FBO_HEIGHT* -0.01f) );
        break;
    case 2:
        //Curse::SECTION2
        //New???
        gl::drawSolidRect( Rectf( FBO_WIDTH* 0.005f, FBO_HEIGHT*(1.00f+ (sin(rotateAmt*0.1)*0.02)), FBO_WIDTH*(1.00f+ (sin(rotateAmt*0.1)*0.0)), FBO_HEIGHT* 0.005f) );

        if(twoPLUS) {
            gl::pushMatrices();
            gl::translate(0,20,0);
            gl::scale(0.85,0.85,0.85);
            gl::drawSolidRect( Rectf( FBO_WIDTH* -0.01f, FBO_HEIGHT*-1.05, FBO_WIDTH*1.01, FBO_HEIGHT*0.05f) );
            gl::popMatrices();
            gl::pushMatrices();
            gl::translate(0,-20,0);
            gl::scale(0.7,0.7,0.7);
            gl::drawSolidRect( Rectf( FBO_WIDTH* -0.01f, FBO_HEIGHT*1.45, FBO_WIDTH*1.01, FBO_HEIGHT* 2.15f) );
            gl::popMatrices();
            glBlendFunc (GL_ONE_MINUS_SRC_COLOR,GL_DST_COLOR);

        }
        break;
    case 3:
        //Curse::SECTION3
        gl::drawSolidRect( Rectf( FBO_WIDTH* 0.001f, FBO_HEIGHT*0.95, FBO_WIDTH*0.999, FBO_HEIGHT* -0.05f) );

        //Lower section
        /*
            gl::enableAlphaBlending(true);
            glColor4f(1,1,1,1.0);
            glVertex2f(FBO_WIDTH*-0.3f,FBO_HEIGHT*1.99+(sin(rotateAmt*0.005f)*1.0));
            glColor4f(1,1,1,1.0);
            glVertex2f(FBO_WIDTH*(1.3- (sin(rotateAmt)*0.00025)),FBO_HEIGHT*1.99+(sin(rotateAmt*0.005f)*1.0));
            glColor4f(1,1,1,0.0);
            glVertex2f(FBO_WIDTH*(1.3- (sin(rotateAmt)*0.00025)),FBO_HEIGHT* 0.9f+(sin(rotateAmt*0.5f)*1));
            glColor4f(1,1,1,0.0);
            glVertex2f(FBO_WIDTH*-0.3f,FBO_HEIGHT* 0.9f+(sin(rotateAmt*0.5f)*1));
            glEnd();
            */
        //gl::
        gl::color(ColorA(1,1,1,1.0));
        //gl::rotate(rotateAmt*-0.3f);
        gl::drawSolidRect( Rectf( FBO_WIDTH* -0.3f, FBO_HEIGHT*1.99, FBO_WIDTH*1.3, FBO_HEIGHT* 0.99f) );
        //gl::rotate(rotateAmt*0.3f);


        /*
            //textureShader.bind();
            glDisable(GL_DEPTH_TEST);
            glEnable(GL_TEXTURE_2D);
            //glDisable(GL_TEXTURE_2D);
            //gl::enableAlphaBlending(true);
            //glBlendFunc(GL_SRC_ALPHA,GL_ONE);
            feedTexture.bind(0);
            feedTexture.enableAndBind();
            //textureShader.uniform("qt_Texture0", 0); // use texture unit 0

            //glUseProgram(textureShader.getHandle());
            //glActiveTexture(GL_TEXTURE0+feedTexture.getId());
            //glBindTexture(GL_TEXTURE_2D,feedTexture.getId());
            //glUniform1i(textureShader.getUniformLocation("qt_Texture0"),feedTexture.getId());
            //gl.activeTexture(gl.TEXTURE0+num);
            //gl.bindTexture(gl.TEXTURE_2D, this.texture);
            //gl.uniform1i(shaderTexturePointer, num);

            gl::VboMesh::VertexIter iter = blendQuad.mapVertexBuffer();
            for( int x = 0; x < 4; ++x ) {
                //positions.at(x) *= 1.001;
                if(x<2) {
                    iter.setColorRGBA(ColorA(1,1,1,1.0));
                } else {
                    iter.setColorRGBA(ColorA(1,1,1,1.0));
                }
                 ++iter;
            }
            //glBindBuffer(GL_ARRAY_BUFFER, blendQuad.getIndexVbo());
            //glVertexAttribPointer(renderShader.texCoords, renderTexCoords.itemSize, gl.FLOAT, false, 0, 0);
            gl::draw(blendQuad);
            gl::disableAlphaBlending();
            //textureShader.unbind();
            */
        break;
    }
    //alternative?
    //gl::drawSolidRect( Rectf( FBO_WIDTH* -0.00f, FBO_HEIGHT*(1+ (sin(rotateAmt)*0.00025)), FBO_WIDTH*(1- (sin(rotateAmt)*0.00025)), FBO_HEIGHT* -0.00f) );

    //This is good shit
    //gl::drawSolidRect( Rectf( FBO_WIDTH* -0.01f, FBO_HEIGHT*(1.01f+ (sin(rotateAmt)*0.00025)), FBO_WIDTH*(1.01f- (sin(rotateAmt)*0.00025)), FBO_HEIGHT* -0.01f) );

    //sec 3?


    feedTexture.unbind(0);

    //rotate back
    gl::translate( FBO_WIDTH*0.5, FBO_HEIGHT*0.5,0);
    switch(Curse::SECTION) {
    case 1:
        //Curse::SECTION1
        gl::rotate(rotateAmt*-0.01f);
        break;
    case 2:
        //Curse::SECTION2
        if(twoPLUS) {
            gl::rotate(rotateAmt*-3.0f);
        } else {
            gl::rotate(rotateAmt*(-1*((float)oscThread->getObjectCount() + 0.01f)));
        }
        break;
    case 3:
        gl::rotate(rotateAmt*-1.0f);
        break;
    }
    gl::translate(FBO_WIDTH* -0.5, FBO_HEIGHT* -0.5,0);

}

void CurseOfYig::feedback2()
{

}


void CurseOfYig::drawObjects()
{

    /*
    //Draw Objects
    glLineWidth(3.0f);
    switch(Curse::SECTION) {
        case 1:
            //Curse::SECTION1
            glColor4f(1,1,1,0.5);
            break;
        case 2:
            //Curse::SECTION 2
            glColor4f(1,1,1,1);
            break;
    }
    //gl::drawStrokedCircle(Vec2f(getMousePos().x,getMousePos().y),40,40);

    //gl::drawStrokedCircle(Vec2f(getMousePos().x,getMousePos().y),70,40);

    switch(Curse::SECTION) {
        case 1:
            //Curse::SECTION1
            glColor4f(0,0,0,0.5);
            glColor4d(0,sin(greenDelta)*0.5+ 0.25,(sin(blueDelta)*0.75)+0.5,0.0025);
            break;
        case 2:
            //Curse::SECTION 2
            glColor4f((sin(redDelta)*0.25)+0.5*0.1,sin(greenDelta)*0.5+ 0.25,(sin(blueDelta)*0.75)+0.5,0.025);
            break;
    }
    */
    //glColor4f(1,1,1,1);
    //gl::drawSolidCircle(Vec2f(getMousePos().x,getMousePos().y),41*fabs(sin(redDelta*0.2)),30);
    //glColor4f(1,1,1,1);


    oscThread->drawObjects();
}

void CurseOfYig::blur()
{
    glDisable(GL_BLEND);
    Area viewport = gl::getViewport();

    // render a simple scene into mFboScene
    gl::setViewport( mFboScene.getBounds() );
    mFboScene.bindFramebuffer();
    gl::pushMatrices();
    gl::setMatricesWindow(FBO_WIDTH, FBO_HEIGHT, false);
    gl::clear( Color::black() );
    gl::draw(feed.getTexture());
    gl::popMatrices();
    mFboScene.unbindFramebuffer();

    // bind the blur shader
    mShaderBlur.bind();
    mShaderBlur.uniform("tex0", 0); // use texture unit 0

    // tell the shader to blur horizontally and the size of 1 pixel
    mShaderBlur.uniform("sampleOffset", Vec2f(1.0f/mFboBlur1.getWidth(), 0.0f));

    //FIRST PASS
    // copy a horizontally blurred version of our scene into the first blur Fbo
    gl::setViewport( mFboBlur1.getBounds() );
    mFboBlur1.bindFramebuffer();
    mFboScene.bindTexture(0);
    gl::pushMatrices();
    gl::setMatricesWindow(FBO_WIDTH, FBO_HEIGHT, false);
    gl::clear( Color::black() );
    gl::drawSolidRect( mFboBlur1.getBounds() );
    gl::popMatrices();
    mFboScene.unbindTexture();
    mFboBlur1.unbindFramebuffer();



    // tell the shader to blur vertically and the size of 1 pixel
    mShaderBlur.uniform("sampleOffset", Vec2f(0.0f, 1.0f/mFboBlur2.getHeight()));

    // copy a vertically blurred version of our blurred scene into the second blur Fbo
    gl::setViewport( mFboBlur2.getBounds() );
    mFboBlur2.bindFramebuffer();
    mFboBlur1.bindTexture(0);
    gl::pushMatrices();
    gl::setMatricesWindow(FBO_WIDTH, FBO_HEIGHT, false);
    gl::clear( Color::black() );
    gl::drawSolidRect( mFboBlur2.getBounds() );
    gl::popMatrices();
    mFboBlur1.unbindTexture();
    mFboBlur2.unbindFramebuffer();

    //SECOND PASS
    // copy a horizontally blurred version of our scene into the first blur Fbo
    gl::setViewport( mFboBlur1.getBounds() );
    mFboBlur1.bindFramebuffer();
    mFboBlur2.bindTexture(0);
    gl::pushMatrices();
    gl::setMatricesWindow(FBO_WIDTH, FBO_HEIGHT, false);
    gl::clear( Color::black() );
    gl::drawSolidRect( mFboBlur1.getBounds() );
    gl::popMatrices();
    mFboBlur2.unbindTexture();
    mFboBlur1.unbindFramebuffer();

    // tell the shader to blur vertically and the size of 1 pixel
    mShaderBlur.uniform("sampleOffset", Vec2f(0.0f, 1.0f/mFboBlur2.getHeight()));

    // copy a vertically blurred version of our blurred scene into the second blur Fbo
    gl::setViewport( mFboBlur2.getBounds() );
    mFboBlur2.bindFramebuffer();
    mFboBlur1.bindTexture(0);
    gl::pushMatrices();
    gl::setMatricesWindow(FBO_WIDTH, FBO_HEIGHT, false);
    gl::clear( Color::black() );
    gl::drawSolidRect( mFboBlur2.getBounds() );
    gl::popMatrices();
    mFboBlur1.unbindTexture();
    mFboBlur2.unbindFramebuffer();

    //Third PASS
    // copy a horizontally blurred version of our scene into the first blur Fbo
    gl::setViewport( mFboBlur1.getBounds() );
    mFboBlur1.bindFramebuffer();
    mFboBlur2.bindTexture(0);
    gl::pushMatrices();
    gl::setMatricesWindow(FBO_WIDTH, FBO_HEIGHT, false);
    gl::clear( Color::black() );
    gl::drawSolidRect( mFboBlur1.getBounds() );
    gl::popMatrices();
    mFboBlur2.unbindTexture();
    mFboBlur1.unbindFramebuffer();

    // tell the shader to blur vertically and the size of 1 pixel
    mShaderBlur.uniform("sampleOffset", Vec2f(0.0f, 1.0f/mFboBlur2.getHeight()));

    // copy a vertically blurred version of our blurred scene into the second blur Fbo
    gl::setViewport( mFboBlur2.getBounds() );
    mFboBlur2.bindFramebuffer();
    mFboBlur1.bindTexture(0);
    gl::pushMatrices();
    gl::setMatricesWindow(FBO_WIDTH, FBO_HEIGHT, false);
    gl::clear( Color::black() );
    gl::drawSolidRect( mFboBlur2.getBounds() );
    gl::popMatrices();
    mFboBlur1.unbindTexture();
    mFboBlur2.unbindFramebuffer();

    // unbind the shader
    mShaderBlur.unbind();

    gl::setViewport( viewport );

    // because the Fbo's have their origin in the LOWER-left corner,
    // flip the Y-axis before drawing
    gl::pushModelView();
    //gl::translate( Vec2f(0, FBO_HEIGHT*0.5) );
    //gl::scale( Vec3f(1, -1, 1) );

    // draw our scene with the blurred version added as a blend
    gl::color( Color::white() );
    gl::draw( mFboScene.getTexture(), getWindowBounds() );

    //Sec3?
    //gl::draw( mFboScene.getTexture(), Rectf( FBO_WIDTH* -0.1f, FBO_HEIGHT*1.99, FBO_WIDTH*(1.01- (sin(rotateAmt)*0.00025)), FBO_HEIGHT* 0.9f) );
    if(Curse::SECTION==1) {
        glBlendFunc (GL_SRC_COLOR,GL_ONE_MINUS_DST_COLOR);
        gl::enableAdditiveBlending();
        gl::draw( mFboBlur2.getTexture(), getWindowBounds() );
    } else if(Curse::SECTION==3) {
        glBlendFunc (GL_SRC_COLOR,GL_ONE);
        gl::enableAdditiveBlending();
        gl::draw( mFboBlur2.getTexture(), getWindowBounds() );
    } else {
        glBlendFunc (GL_SRC_COLOR,GL_ONE);
        gl::enableAdditiveBlending();
        gl::draw( mFboBlur2.getTexture(), getWindowBounds() );
    }
    gl::draw( mFboBlur2.getTexture(), getWindowBounds() );
    //Sec3?
    //gl::draw( mFboBlur2.getTexture(), Rectf( FBO_WIDTH* -0.1f, FBO_HEIGHT*1.99, FBO_WIDTH*(1.01- (sin(rotateAmt)*0.00025)), FBO_HEIGHT* 0.9f));

    gl::disableAlphaBlending();

    // restore the modelview matrix
    gl::popModelView();

    //gl::enableAdditiveBlending();
    //gl::color( Color::white() );
    //gl::draw( blurBuffer.getTexture(), getWindowBounds() );
    //gl::disableAlphaBlending();
}
CINDER_APP_BASIC( CurseOfYig, RendererGl )


